package com.my.sender;

import com.google.gson.JsonObject;
import com.my.pojo.Goods;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 消息发送者
 */
@Component
public class ProductSender {

    private AmqpTemplate rabbitAmqpTemplate;

    @Autowired
    public void setRabbitAmqpTemplate(AmqpTemplate rabbitAmqpTemplate) {
        this.rabbitAmqpTemplate = rabbitAmqpTemplate;
    }

    //交换器名称
    @Value("${mq.config.stock.exchange}")
    private String stockExchange;
    //路由键
    @Value("${mq.config.queue.stock.push}")
    private String stockRoutingKey;


    /**
     * 发送消息的方法
     */
    public void stockAdd(Integer goods_id, Integer num, String msg) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("goods_id", goods_id);
        jsonObject.addProperty("num", num);
        jsonObject.addProperty("msg", msg);
        System.out.println(jsonObject);
        rabbitAmqpTemplate.convertAndSend(stockExchange, stockRoutingKey,jsonObject.toString());
    }

}
